package edu.usc.cmb.iRegulome.action;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;

import edu.usc.cmb.iregulome.dao.SampleStudyDAO;
import edu.usc.cmb.iregulome.entity.ChIPseqTableEntity;
import edu.usc.cmb.iregulome.entity.Sample;
import edu.usc.cmb.iregulome.entity.SampleAttribute;
import edu.usc.cmb.iregulome.entity.Study;

public class SampleDetailAction extends ActionWithSampleStudyDAO {

	private String sampleID;

	private Sample sampleDetail;

	private Study studyDetail;

	private String sampleIDs;

	private ChIPseqTableEntity chipSeqStat;

	private List<SampleAttribute> attrs;

	private List<Sample> sampleList;

	private Map<String, ChIPseqTableEntity> chipSeqStatMap;

	public SampleDetailAction(SampleStudyDAO sampleStudyDAO) {
		super(sampleStudyDAO);
	}

	public void setSampleID(String sampleID) {
		this.sampleID = sampleID;
	}

	public String getSampleID() {
		return sampleID;
	}

	public void setStudyDetail(Study studyDetail) {
		this.studyDetail = studyDetail;
	}

	public Study getStudyDetail() {
		return studyDetail;
	}

	public void setSampleDetail(Sample sampleDetail) {
		this.sampleDetail = sampleDetail;
	}

	public Sample getSampleDetail() {
		return sampleDetail;
	}

	public String fetchSampleDetail() {
		setSampleDetail(getSampleStudyDAO().fetchSample(getSampleID()));
		setStudyDetail(getSampleStudyDAO().fetchStudyBySampleID(getSampleID()));
		setChipSeqStat(getSampleStudyDAO().fetchChIPSeqStat(getSampleID()));
		setAttrs(getSampleStudyDAO().fetchSampleAttributes(getSampleID()));
		return Action.SUCCESS;
	}

	public String showGroup() {
		String[] sampleids = sampleIDs.split(",");
		setSampleList(getSampleStudyDAO()
				.fetchSamples(Arrays.asList(sampleids)));
		Map<String, ChIPseqTableEntity> statMap = new HashMap<String, ChIPseqTableEntity>();
		List<ChIPseqTableEntity> l = getSampleStudyDAO().fetchChIPSeqStatList(
				Arrays.asList(sampleids));
		if(l!=null){
			for (ChIPseqTableEntity chIPseqTableEntity : l) {
				statMap.put(chIPseqTableEntity.getSampleID(), chIPseqTableEntity);
			}
		}
		setChipSeqStatMap(statMap);
		return Action.SUCCESS;
	}

	public void setChipSeqStat(ChIPseqTableEntity chipSeqStat) {
		this.chipSeqStat = chipSeqStat;
	}

	public ChIPseqTableEntity getChipSeqStat() {
		return chipSeqStat;
	}

	public void setAttrs(List<SampleAttribute> attrs) {
		this.attrs = attrs;
	}

	public List<SampleAttribute> getAttrs() {
		return attrs;
	}

	public void setSampleIDs(String sampleIDs) {
		this.sampleIDs = sampleIDs;
	}

	public String getSampleIDs() {
		return sampleIDs;
	}


	public void setSampleList(List<Sample> sampleList) {
		this.sampleList = sampleList;
	}

	public List<Sample> getSampleList() {
		return sampleList;
	}

	public void setChipSeqStatMap(Map<String, ChIPseqTableEntity> chipSeqStatMap) {
		this.chipSeqStatMap = chipSeqStatMap;
	}

	public Map<String, ChIPseqTableEntity> getChipSeqStatMap() {
		return chipSeqStatMap;
	}

}
